﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using OgilvyOne.BLL;
using OgilvyOne.Model;
using System.IO;
using Lucene.Net;
using Lucene.Net.Index;
using Lucene.Net.Documents;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Analysis.Cn;
using Lucene.Net.Highlight;

public partial class admin_allergy_info_keywords : System.Web.UI.Page
{
    public int isaudit = 0;
    public static readonly string ArticleVideoLayerids = OgilvyOne.Utility.ConfigUtil.GetStringValue("ArticleVideoLayerids");
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        {
            this.headers.Text = new ColumnBll().LayeridColumnConNaiv(Request.Params["layerid"]);
            Grid_viewBild();
        }
    }

    private void Grid_viewBild()
    {
        int columnid = int.Parse(Request.Params["columnid"]);
        int pagesize = 10;


        if (Request.Params["rows"] != null && Request.Params["rows"] != "")
        {
            pagesize = int.Parse(Request.Params["rows"]);
            //this.rows.Text = pagesize.ToString();
        }

        if (Request.Params["pageNum"] != null && Request.Params["pageNum"] != "")
        {
            try
            {
                GridView1.PageIndex = int.Parse(Request.Params["pageNum"]) - 1;
            }
            catch { }
        }
        DataTable dt = BLLArticleInfo.getallkeywords();
        GridView1.DataSource = dt;
        GridView1.PageSize = pagesize;
        GridView1.DataBind();
        this.pagehtml.Text = getPageHtml(GridView1.PageCount, GridView1.PageIndex + 1, BLLArticleInfo.countkeywords());
    }

    protected void Add_click(object sender, EventArgs e)
    {
        Response.Redirect("info_keywords_detail.aspx?columnid=" + Request.Params["columnid"] + "&layerid=" + Request.Params["layerid"], true);
    }

    protected void index_click(object sender, EventArgs e)
    {
        CreateIndex("../../allergy/search/reader/");
    }
    public void CreateIndex(string path)
    {
        SqlDataReader Dr = GetDR();
        IndexWriter writer = new IndexWriter(Server.MapPath(path), new ChineseAnalyzer(), true);
        writer.SetMergeFactor(20);  //      调整segment合并的频率和大小
        while (Dr.Read())
        {

            Document doc = GetDocumentObj(Dr["ARTICLEID"].ToString(), Dr["TITLE"].ToString(), Dr["SUMMARY"].ToString(), Dr["ADD_TIME"].ToString(), Dr["parentename"].ToString(), Dr["childenamel"].ToString(), Dr["STATICPAGENAME"].ToString());
            //    doc.SetBoost(1.5);
            //写入索引文档
            writer.AddDocument(doc);

        }
        Dr.Close();
        //优化文档
        writer.Optimize();
        //关闭
        writer.Close();
    }
    public SqlDataReader GetDR()
    {
        SqlConnection Conn = new SqlConnection("server=10.172.12.42;uid=nestle_allergy;password=nestle_allergy;database=nestle_allergy;min pool size=4;min pool size=4;max pool size=100;packet size=3072");
        //SqlConnection Conn = new SqlConnection("server=HUNTER\\HUNTER05;uid=sa;password=123@qwe;database=nestle;min pool size=4;min pool size=4;max pool size=100;packet size=3072");
        SqlCommand Cmd = new SqlCommand();
        Cmd.Connection = Conn;
        Conn.Open();
        Cmd.CommandText = string.Format("select  * from CMS_ARTICLE");
        SqlDataReader Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
        return Dr;

    }
    public Document GetDocumentObj(string articleId, string Title, string Content, string Add_Time, string parentename, string childenamel,string staticpname)
    {
        Document doc = new Document();
        doc.Add(new Field("ARTICLEID", articleId, Field.Store.YES, Field.Index.UN_TOKENIZED));
        doc.Add(new Field("TITLE", Title, Field.Store.YES, Field.Index.TOKENIZED));
        doc.Add(new Field("SUMMARY", Content, Field.Store.YES, Field.Index.TOKENIZED));
        doc.Add(new Field("ADD_TIME", Add_Time, Field.Store.YES, Field.Index.UN_TOKENIZED));
        doc.Add(new Field("parentename", parentename, Field.Store.YES, Field.Index.TOKENIZED));
        doc.Add(new Field("childenamel", childenamel, Field.Store.YES, Field.Index.TOKENIZED));
        doc.Add(new Field("STATICPAGENAME", staticpname, Field.Store.YES, Field.Index.TOKENIZED));
        return doc;
    }






    protected void Del_click(object sender, EventArgs e)
    {
        string ids = Request.Params["ids"];
        bool r = BLLArticleInfo.Delkeywords(ids);
        Grid_viewBild();
    }

    protected void Mod_click(object sender, EventArgs e)
    {
        Response.Redirect("info_keywords_detail.aspx?columnid=" + Request.Params["columnid"] + "&layerid=" + Request.Params["layerid"] + "&id=" + ((LinkButton)sender).CommandArgument, true);
    }
    public string getPageHtml(int pageCount, int pageNum, int totalCount)
    {
        StringBuilder html = new StringBuilder();

        if (totalCount > 0)
        {
            int nextPage = pageNum + 1 > pageCount ? pageCount : pageNum + 1;
            int prevPage = pageNum - 1 < 1 ? 1 : pageNum - 1;

            html.Append("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" border=\"1\">");
            html.Append("<tr>");
            html.Append("<td height=\"30\" align=\"center\">");
            html.Append("第 <b>" + pageNum + "</b>/<b>" + pageCount + "</b> 页 ");
            html.Append("总记录 <b>" + totalCount + "</b>&nbsp;&nbsp;&nbsp;");
            //
            if (pageNum == 1)
            {
                html.Append("首页 上一页");
            }
            else
            {
                html.Append("<a href=\"javascript:void(0);\" onclick=\"document.forms[0].pageNum.value=1;document.forms[0].submit();return false;\">首页</a>");
                html.Append(" <a href=\"javascript:void(0);\" onclick=\"document.forms[0].pageNum.value=" + prevPage + ";document.forms[0].submit();return false;\" >上一页</a> ");
            }
            if (pageNum >= pageCount)
            {
                html.Append("下一页  末页");
            }
            else
            {
                html.Append("<a href=\"javascript:void(0);\" onclick=\"document.forms[0].pageNum.value=" + nextPage + ";document.forms[0].submit();return false;\" >下一页</a>");
                html.Append(" <a href=\"javascript:void(0);\" onclick=\"document.forms[0].pageNum.value=" + (pageCount) + ";document.forms[0].submit();return false;\">末页</a>");
            }
            html.Append("&nbsp;<input name=\"pageNum\" type=\"text\" style=\"HEIGHT: 18px; WIDTH: 22px;\" maxlength=\"4\" value=\"" + pageNum + "\" />");
            html.Append("<input name=\"Go\" type=\"button\" value=\"Go\" onclick=\"javascript:document.forms[0].submit();return false;\" style=\"HEIGHT: 18px; WIDTH: 22px;\" />");
            html.Append("</td>");
            html.Append("</tr>");
            html.Append("</table>");
        }
        else
        {
            html.Append("<input type=\"hidden\" id=\"pageNum\" name=\"pageNum\" value=\"" + pageNum + "\" />");
        }

        return html.ToString();
    }

}
